<?php

class CountrySelect extends Block{

	public $templatePath = __DIR__ ;
	public $templateFile = "CountrySelect.tpl.std.html";
	
	//Tag Attributes
	public $caption     = "TextBox";
	public $description = "";
	public $value       = "";
	public $size        = "20";
	public $name        = "";
	public $style       = "";
	public $title       = "";
	public $class       = "";
	public $type        = "text";
	public $readonly    = "";
	public $disabled    = "";
	public $accesskey   = "";
	public $maxlength   = 255;
	
	//Events
	public $onfocus;
	public $onblur;
	public $onselect;
	public $onchange;
	public $onclick;
	public $ondblclick;
	public $onmousedown;
	public $onmouseup;
	public $onmouseover;
	public $onmousemove;
	public $onmouseout;
	public $onkeypress;
	public $onkeydown;
	public $onkeyup;
	
	
	
	public function show(){
		//echo "<pre>";print_r($this);	
		$this->parseFromFile($this->templatePath."/".$this->templateFile);
		
		if(empty($this->name)){
			$this->name=$this->oid;
		}
			
		$this->events();
		$this->countries();
		
		return $this->replace();
	}
	
	private function events(){
	
		$events = array(
			"onfocus","onblur","onselect","onchange","onclick","ondblclick"	
			,"onmousedown","onmouseup","onmouseover","onmousemove","onmouseout"
			,"onkeypress","onkeydown","onkeyup"
		);
		
		$out="";
		foreach($events as $name){
			if(isset($this->$name) && !empty($this->$name)){
				
				$this->event->name  = $name;
				$this->event->value = $this->$name;
				
				$out.= $this->event->replace();
			}
		}
		$this->event->block=$out;
	}
	
	private function countries(){
		$out="";
		if(file_exists( __DIR__."/country.txt")){
			$countries=file(__DIR__."/country.txt");
			
			foreach($countries as $country){
				list($code,$text)=explode("|",$country);
				$code=trim($code);
				$text=trim($text);
				
				$this->options->code = $code;
				$this->options->text = $text;
				
				if($this->value==$code){
					$this->options->selected="selected='selected'";
				}else{
					$this->options->selected="";
				}
				$out.=$this->options->replace();
			}
			$this->options->block=$out;
			
		}
		return $out;
	}
	
}

?>